﻿
namespace SolutionFramework.Entities
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using SolutionFramework.Entities;
    using AbstraX.Contracts;
    using AbstraX.ServerInterfaces;

    // Implements application logic using the SolutionFrameworkEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class SolutionFrameworkDomainService : LinqToEntitiesDomainService<SolutionFrameworkEntities>, IAbstraXService
    {
        public IDomainHostApplication DomainServiceHostApplication { get; set; }

        public override void Initialize(DomainServiceContext context)
        {
            base.Initialize(context);
        }

        [Query]
        public IQueryable<ProviderListItem> GetProviderList()
        {
            return this.DomainServiceHostApplication.ProviderList.AsQueryable();
        }

        [Delete]
        public void DeleteProvider(ProviderListItem listItem)
        {
        }

        [Update]
        public void UpdateProvider(ProviderListItem listItem)
        {
        }

        [Insert]
        public void InsertProvider(ProviderListItem listItem)
        {
        }

        [Invoke]
        public void BuildTree(string entityID)
        {
        }

        [Invoke]
        public bool TreeBuilderOrphansExist()
        {
            var context = new SolutionFrameworkDataContext();

            return (bool) context.TreeBuilderOrphansExist();
        }

        public IQueryable<Application> GetApplications()
        {
            return this.ObjectContext.Applications;
        }

        [Invoke]
        public IEnumerable<string> GetStatusMessages()
        {
            foreach (string message in DomainServiceHostApplication.LatestStatus)
            {
                yield return message;
            }
        }

        public string GetImageEditorUrl()
        {
            return DomainServiceHostApplication.GetImageEditorUrl();
        }

        public bool HasImagePropertyBinding(TreeNodePropertyBinding propertyBinding)
        {
            return this.ObjectContext.TreeNodeImages.Any(i => i.TreeNodeBaseID == propertyBinding.TreeNodeBaseID);
        }

        public bool HasImageMenuItem(TreeNodeMenuItem menuItem)
        {
            return this.ObjectContext.TreeNodeMenuItems.Any(i => i.TreeNodeBaseID == menuItem.TreeNodeBaseID);
        }

        public byte[] GetImage(string key)
        {
            return null;
        }

        public void InsertApplication(Application application)
        {
            if ((application.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(application, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Applications.AddObject(application);
            }
        }

        public void UpdateApplication(Application currentApplication)
        {
            this.ObjectContext.Applications.AttachAsModified(currentApplication, this.ChangeSet.GetOriginal(currentApplication));
        }

        public void DeleteApplication(Application application)
        {
            if ((application.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(application, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Applications.Attach(application);
                this.ObjectContext.Applications.DeleteObject(application);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'ApplicationNodes' query.
        [Query(IsDefault = true)]
        public IQueryable<ApplicationNode> GetApplicationNodes()
        {
            return this.ObjectContext.ApplicationNodes;
        }

        public void InsertApplicationNode(ApplicationNode applicationNode)
        {
            if ((applicationNode.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(applicationNode, EntityState.Added);
            }
            else
            {
                this.ObjectContext.ApplicationNodes.AddObject(applicationNode);
            }
        }

        public void UpdateApplicationNode(ApplicationNode currentApplicationNode)
        {
            this.ObjectContext.ApplicationNodes.AttachAsModified(currentApplicationNode, this.ChangeSet.GetOriginal(currentApplicationNode));
        }

        public void DeleteApplicationNode(ApplicationNode applicationNode)
        {
            if ((applicationNode.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(applicationNode, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.ApplicationNodes.Attach(applicationNode);
                this.ObjectContext.ApplicationNodes.DeleteObject(applicationNode);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'ApplicationRoots' query.
        [Query(IsDefault = true)]
        public IQueryable<ApplicationRoot> GetApplicationRoots()
        {
            return this.ObjectContext.ApplicationRoots;
        }

        public void InsertApplicationRoot(ApplicationRoot applicationRoot)
        {
            if ((applicationRoot.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(applicationRoot, EntityState.Added);
            }
            else
            {
                this.ObjectContext.ApplicationRoots.AddObject(applicationRoot);
            }
        }

        public void UpdateApplicationRoot(ApplicationRoot currentApplicationRoot)
        {
            this.ObjectContext.ApplicationRoots.AttachAsModified(currentApplicationRoot, this.ChangeSet.GetOriginal(currentApplicationRoot));
        }

        public void DeleteApplicationRoot(ApplicationRoot applicationRoot)
        {
            if ((applicationRoot.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(applicationRoot, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.ApplicationRoots.Attach(applicationRoot);
                this.ObjectContext.ApplicationRoots.DeleteObject(applicationRoot);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'DevProjectInformations' query.
        [Query(IsDefault = true)]
        public IQueryable<DevProjectInformation> GetDevProjectInformations()
        {
            return this.ObjectContext.DevProjectInformations;
        }

        public void InsertDevProjectInformation(DevProjectInformation devProjectInformation)
        {
            if ((devProjectInformation.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(devProjectInformation, EntityState.Added);
            }
            else
            {
                this.ObjectContext.DevProjectInformations.AddObject(devProjectInformation);
            }
        }

        public void UpdateDevProjectInformation(DevProjectInformation currentDevProjectInformation)
        {
            this.ObjectContext.DevProjectInformations.AttachAsModified(currentDevProjectInformation, this.ChangeSet.GetOriginal(currentDevProjectInformation));
        }

        public void DeleteDevProjectInformation(DevProjectInformation devProjectInformation)
        {
            if ((devProjectInformation.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(devProjectInformation, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.DevProjectInformations.Attach(devProjectInformation);
                this.ObjectContext.DevProjectInformations.DeleteObject(devProjectInformation);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Products' query.
        [Query(IsDefault = true)]
        public IQueryable<Product> GetProducts()
        {
            return this.ObjectContext.Products;
        }

        public void InsertProduct(Product product)
        {
            if ((product.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(product, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Products.AddObject(product);
            }
        }

        public void UpdateProduct(Product currentProduct)
        {
            this.ObjectContext.Products.AttachAsModified(currentProduct, this.ChangeSet.GetOriginal(currentProduct));
        }

        public void DeleteProduct(Product product)
        {
            if ((product.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(product, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Products.Attach(product);
                this.ObjectContext.Products.DeleteObject(product);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'TreeBuilderRoots' query.
        [Query(IsDefault = true)]
        public IQueryable<TreeBuilderRoot> GetTreeBuilderRoots()
        {
            return this.ObjectContext.TreeBuilderRoots.Include("TreeNodeBase");
        }

        public void InsertTreeBuilderRoot(TreeBuilderRoot treeBuilderRoot)
        {
            if ((treeBuilderRoot.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeBuilderRoot, EntityState.Added);
            }
            else
            {
                this.ObjectContext.TreeBuilderRoots.AddObject(treeBuilderRoot);
            }
        }

        public void UpdateTreeBuilderRoot(TreeBuilderRoot currentTreeBuilderRoot)
        {
            var orig = this.ChangeSet.GetOriginal(currentTreeBuilderRoot);

            if (orig == null)
            {
                this.ObjectContext.TreeBuilderRoots.AttachAsModified(currentTreeBuilderRoot);
            }
            else
            {
                this.ObjectContext.TreeBuilderRoots.AttachAsModified(currentTreeBuilderRoot, orig);
            }
        }

        public void DeleteTreeBuilderRoot(TreeBuilderRoot treeBuilderRoot)
        {
            if ((treeBuilderRoot.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeBuilderRoot, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.TreeBuilderRoots.Attach(treeBuilderRoot);
                this.ObjectContext.TreeBuilderRoots.DeleteObject(treeBuilderRoot);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'TreeNodeAdvancedQueries' query.
        [Query(IsDefault = true)]
        public IQueryable<TreeNodeAdvancedQuery> GetTreeNodeAdvancedQueries()
        {
            return this.ObjectContext.TreeNodeAdvancedQueries.Include("TreeNodeBase");
        }

        public void InsertTreeNodeAdvancedQuery(TreeNodeAdvancedQuery treeNodeAdvancedQuery)
        {
            if ((treeNodeAdvancedQuery.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeAdvancedQuery, EntityState.Added);
            }
            else
            {
                this.ObjectContext.TreeNodeAdvancedQueries.AddObject(treeNodeAdvancedQuery);
            }
        }

        public void UpdateTreeNodeAdvancedQuery(TreeNodeAdvancedQuery currentTreeNodeAdvancedQuery)
        {
            var orig = this.ChangeSet.GetOriginal(currentTreeNodeAdvancedQuery);

            if (orig == null)
            {
                this.ObjectContext.TreeNodeAdvancedQueries.AttachAsModified(currentTreeNodeAdvancedQuery);
            }
            else
            {
                this.ObjectContext.TreeNodeAdvancedQueries.AttachAsModified(currentTreeNodeAdvancedQuery, orig);
            }
        }

        public void DeleteTreeNodeAdvancedQuery(TreeNodeAdvancedQuery treeNodeAdvancedQuery)
        {
            if ((treeNodeAdvancedQuery.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeAdvancedQuery, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.TreeNodeAdvancedQueries.Attach(treeNodeAdvancedQuery);
                this.ObjectContext.TreeNodeAdvancedQueries.DeleteObject(treeNodeAdvancedQuery);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'TreeBuilderRoots' query.
        [Query(IsDefault = true)]
        public IQueryable<TreeNodeTransferMapping> GetTreeNodeTransferMappings()
        {
            return this.ObjectContext.TreeNodeTransferMappings.Include("TreeNodeBase");
        }

        public void InsertTreeNodeTransferMapping(TreeNodeTransferMapping treeNodeTransferMapping)
        {
            if ((treeNodeTransferMapping.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeTransferMapping, EntityState.Added);
            }
            else
            {
                this.ObjectContext.TreeNodeTransferMappings.AddObject(treeNodeTransferMapping);
            }
        }

        public void UpdateTreeNodeTransferMapping(TreeNodeTransferMapping currentTreeNodeTransferMapping)
        {
            var orig = this.ChangeSet.GetOriginal(currentTreeNodeTransferMapping);

            if (orig == null)
            {
                this.ObjectContext.TreeNodeTransferMappings.AttachAsModified(currentTreeNodeTransferMapping);
            }
            else
            {
                this.ObjectContext.TreeNodeTransferMappings.AttachAsModified(currentTreeNodeTransferMapping, orig);
            }
        }

        public void DeleteTreeNodeTransferMapping(TreeNodeTransferMapping treeNodeTransferMapping)
        {
            if ((treeNodeTransferMapping.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeTransferMapping, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.TreeNodeTransferMappings.Attach(treeNodeTransferMapping);
                this.ObjectContext.TreeNodeTransferMappings.DeleteObject(treeNodeTransferMapping);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'TreeBuilderRoots' query.
        [Query(IsDefault = true)]
        public IQueryable<TreeNodeRecycleBin> GetTreeNodeRecycleBins()
        {
            return this.ObjectContext.TreeNodeRecycleBins.Include("TreeNodeBase");
        }

        public void InsertTreeNodeRecycleBin(TreeNodeRecycleBin treeNodeRecycleBin)
        {
            if ((treeNodeRecycleBin.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeRecycleBin, EntityState.Added);
            }
            else
            {
                this.ObjectContext.TreeNodeRecycleBins.AddObject(treeNodeRecycleBin);
            }
        }

        public void UpdateTreeNodeRecycleBin(TreeNodeRecycleBin currentTreeNodeRecycleBin)
        {
            var orig = this.ChangeSet.GetOriginal(currentTreeNodeRecycleBin);

            if (orig == null)
            {
                this.ObjectContext.TreeNodeRecycleBins.AttachAsModified(currentTreeNodeRecycleBin);
            }
            else
            {
                this.ObjectContext.TreeNodeRecycleBins.AttachAsModified(currentTreeNodeRecycleBin, orig);
            }
        }

        public void DeleteTreeNodeRecycleBin(TreeNodeRecycleBin treeNodeRecycleBin)
        {
            if ((treeNodeRecycleBin.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeRecycleBin, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.TreeNodeRecycleBins.Attach(treeNodeRecycleBin);
                this.ObjectContext.TreeNodeRecycleBins.DeleteObject(treeNodeRecycleBin);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'TreeNodeAttributeNodes' query.
        [Query(IsDefault = true)]
        public IQueryable<TreeNodeAttributeNode> GetTreeNodeAttributeNodes()
        {
            return this.ObjectContext.TreeNodeAttributeNodes.Include("TreeNodeBase");
        }

        public void InsertTreeNodeAttributeNode(TreeNodeAttributeNode treeNodeAttributeNode)
        {
            if ((treeNodeAttributeNode.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeAttributeNode, EntityState.Added);
            }
            else
            {
                this.ObjectContext.TreeNodeAttributeNodes.AddObject(treeNodeAttributeNode);
            }
        }

        public void UpdateTreeNodeAttributeNode(TreeNodeAttributeNode currentTreeNodeAttributeNode)
        {
            var orig = this.ChangeSet.GetOriginal(currentTreeNodeAttributeNode);

            if (orig == null)
            {
                this.ObjectContext.TreeNodeAttributeNodes.AttachAsModified(currentTreeNodeAttributeNode);
            }
            else
            {
                this.ObjectContext.TreeNodeAttributeNodes.AttachAsModified(currentTreeNodeAttributeNode, orig);
            }
        }

        public void DeleteTreeNodeAttributeNode(TreeNodeAttributeNode treeNodeAttributeNode)
        {
            if ((treeNodeAttributeNode.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeAttributeNode, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.TreeNodeAttributeNodes.Attach(treeNodeAttributeNode);
                this.ObjectContext.TreeNodeAttributeNodes.DeleteObject(treeNodeAttributeNode);
            }
        }


        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'TreeNodeAttributeNodes' query.
        [Query(IsDefault = true)]
        public IQueryable<TreeNodeMenuItem> GetTreeNodeMenuItems()
        {
            return this.ObjectContext.TreeNodeMenuItems.Include("TreeNodeBase");
        }

        public void InsertTreeNodeMenuItem(TreeNodeMenuItem treeNodeMenuItem)
        {
            if ((treeNodeMenuItem.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeMenuItem, EntityState.Added);
            }
            else
            {
                this.ObjectContext.TreeNodeMenuItems.AddObject(treeNodeMenuItem);
            }
        }

        public void UpdateTreeNodeMenuItem(TreeNodeMenuItem currentTreeNodeMenuItem)
        {
            var orig = this.ChangeSet.GetOriginal(currentTreeNodeMenuItem);

            if (orig == null)
            {
                this.ObjectContext.TreeNodeMenuItems.AttachAsModified(currentTreeNodeMenuItem);
            }
            else
            {
                this.ObjectContext.TreeNodeMenuItems.AttachAsModified(currentTreeNodeMenuItem, orig);
            }
        }

        public void DeleteTreeNodeMenuItem(TreeNodeMenuItem treeNodeMenuItem)
        {
            if ((treeNodeMenuItem.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeMenuItem, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.TreeNodeMenuItems.Attach(treeNodeMenuItem);
                this.ObjectContext.TreeNodeMenuItems.DeleteObject(treeNodeMenuItem);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'TreeNodeAttributeNodes' query.
        [Query(IsDefault = true)]
        public IQueryable<TreeNodeImage> GetTreeNodeImages()
        {
            return this.ObjectContext.TreeNodeImages.Include("TreeNodeBase");
        }

        public void InsertTreeNodeImage(TreeNodeImage treeNodeImage)
        {
            if ((treeNodeImage.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeImage, EntityState.Added);
            }
            else
            {
                this.ObjectContext.TreeNodeImages.AddObject(treeNodeImage);
            }
        }

        public void UpdateTreeNodeImage(TreeNodeImage currentTreeNodeImage)
        {
            var orig = this.ChangeSet.GetOriginal(currentTreeNodeImage);

            if (orig == null)
            {
                this.ObjectContext.TreeNodeImages.AttachAsModified(currentTreeNodeImage);
            }
            else
            {
                this.ObjectContext.TreeNodeImages.AttachAsModified(currentTreeNodeImage, orig);
            }
        }

        public void DeleteTreeNodeImage(TreeNodeImage treeNodeImage)
        {
            if ((treeNodeImage.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeImage, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.TreeNodeImages.Attach(treeNodeImage);
                this.ObjectContext.TreeNodeImages.DeleteObject(treeNodeImage);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'TreeNodeOperationNodes' query.
        public IQueryable<TreeNodeOperationNode> GetTreeNodeOperationNodes()
        {
            return this.ObjectContext.TreeNodeOperationNodes.Include("TreeNodeBase");
        }

        public void InsertTreeNodeOperationNode(TreeNodeOperationNode treeNodeOperationNode)
        {
            if ((treeNodeOperationNode.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeOperationNode, EntityState.Added);
            }
            else
            {
                this.ObjectContext.TreeNodeOperationNodes.AddObject(treeNodeOperationNode);
            }
        }

        public void UpdateTreeNodeOperationNode(TreeNodeOperationNode currentTreeNodeOperationNode)
        {
            var orig = this.ChangeSet.GetOriginal(currentTreeNodeOperationNode);

            if (orig == null)
            {
                this.ObjectContext.TreeNodeOperationNodes.AttachAsModified(currentTreeNodeOperationNode);
            }
            else
            {
                this.ObjectContext.TreeNodeOperationNodes.AttachAsModified(currentTreeNodeOperationNode, orig);
            }
        }

        public void DeleteTreeNodeOperationNode(TreeNodeOperationNode treeNodeOperationNode)
        {
            if ((treeNodeOperationNode.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeOperationNode, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.TreeNodeOperationNodes.Attach(treeNodeOperationNode);
                this.ObjectContext.TreeNodeOperationNodes.DeleteObject(treeNodeOperationNode);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'TreeNodeBases' query.
        [Query(IsDefault = true)]
        public IQueryable<TreeNodeBase> GetTreeNodeBases()
        {
            return this.ObjectContext.TreeNodeBases;
        }

        public void InsertTreeNodeBase(TreeNodeBase treeNodeBase)
        {
            if ((treeNodeBase.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeBase, EntityState.Added);
            }
            else
            {
                this.ObjectContext.TreeNodeBases.AddObject(treeNodeBase);
            }
        }

        public void UpdateTreeNodeBase(TreeNodeBase currentTreeNodeBase)
        {
            this.ObjectContext.TreeNodeBases.AttachAsModified(currentTreeNodeBase);
        }

        public void DeleteTreeNodeBase(TreeNodeBase treeNodeBase)
        {
            if ((treeNodeBase.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeBase, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.TreeNodeBases.Attach(treeNodeBase);
                this.ObjectContext.TreeNodeBases.DeleteObject(treeNodeBase);
            }
        }

        public int GetItemBindingPropertyTypes(Guid parentItemID)
        {
            int types = 0;

            this.ObjectContext.TreeNodePropertyBindings.Where(b => b.TreeNodeBase.ParentNodeID == parentItemID).ToList().ForEach(b => types |= b.PropertyType);

            return types;
        }

        public int GetItemTransferCreationMappingTypes(Guid parentItemID)
        {
            int types = 0;

            this.ObjectContext.TreeNodeTransferMappings.Where(b => b.TreeNodeBase.ParentNodeID == parentItemID).ToList().ForEach(m => types |= m.TransferMappingType);

            return types;
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'TreeNodeElementNodes' query.
        [Query(IsDefault = true)]
        public IQueryable<TreeNodeElementNode> GetTreeNodeElementNodes()
        {
            return this.ObjectContext.TreeNodeElementNodes.Include("TreeNodeBase");
        }

        public void InsertTreeNodeElementNode(TreeNodeElementNode treeNodeElementNode)
        {
            if ((treeNodeElementNode.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeElementNode, EntityState.Added);
            }
            else
            {
                this.ObjectContext.TreeNodeElementNodes.AddObject(treeNodeElementNode);
            }
        }

        public void UpdateTreeNodeElementNode(TreeNodeElementNode currentTreeNodeElementNode)
        {
            var orig = this.ChangeSet.GetOriginal(currentTreeNodeElementNode);

            if (orig == null)
            {
                this.ObjectContext.TreeNodeElementNodes.AttachAsModified(currentTreeNodeElementNode);
            }
            else
            {
                this.ObjectContext.TreeNodeElementNodes.AttachAsModified(currentTreeNodeElementNode, orig);
            }
        }

        public void DeleteTreeNodeElementNode(TreeNodeElementNode treeNodeElementNode)
        {
            if ((treeNodeElementNode.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeElementNode, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.TreeNodeElementNodes.Attach(treeNodeElementNode);
                this.ObjectContext.TreeNodeElementNodes.DeleteObject(treeNodeElementNode);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'TreeNodeFolders' query.
        [Query(IsDefault = true)]
        public IQueryable<TreeNodeFolder> GetTreeNodeFolders()
        {
            return this.ObjectContext.TreeNodeFolders.Include("TreeNodeBase");
        }

        public void InsertTreeNodeFolder(TreeNodeFolder treeNodeFolder)
        {
            if ((treeNodeFolder.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeFolder, EntityState.Added);
            }
            else
            {
                this.ObjectContext.TreeNodeFolders.AddObject(treeNodeFolder);
            }
        }

        public void UpdateTreeNodeFolder(TreeNodeFolder currentTreeNodeFolder)
        {
            var orig = this.ChangeSet.GetOriginal(currentTreeNodeFolder);

            if (orig == null)
            {
                this.ObjectContext.TreeNodeFolders.AttachAsModified(currentTreeNodeFolder);
            }
            else
            {
                this.ObjectContext.TreeNodeFolders.AttachAsModified(currentTreeNodeFolder, orig);
            }
        }

        public void DeleteTreeNodeFolder(TreeNodeFolder treeNodeFolder)
        {
            if ((treeNodeFolder.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeFolder, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.TreeNodeFolders.Attach(treeNodeFolder);
                this.ObjectContext.TreeNodeFolders.DeleteObject(treeNodeFolder);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'TreeNodePropertyBindings' query.
        [Query(IsDefault = true)]
        public IQueryable<TreeNodePropertyBinding> GetTreeNodePropertyBindings()
        {
            return this.ObjectContext.TreeNodePropertyBindings.Include("TreeNodeBase");
        }

        public void InsertTreeNodePropertyBinding(TreeNodePropertyBinding treeNodePropertyBinding)
        {
            if ((treeNodePropertyBinding.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodePropertyBinding, EntityState.Added);
            }
            else
            {
                this.ObjectContext.TreeNodePropertyBindings.AddObject(treeNodePropertyBinding);
            }
        }

        public void UpdateTreeNodePropertyBinding(TreeNodePropertyBinding currentTreeNodePropertyBinding)
        {
            var orig = this.ChangeSet.GetOriginal(currentTreeNodePropertyBinding);

            if (orig == null)
            {
                this.ObjectContext.TreeNodePropertyBindings.AttachAsModified(currentTreeNodePropertyBinding);
            }
            else
            {
                this.ObjectContext.TreeNodePropertyBindings.AttachAsModified(currentTreeNodePropertyBinding, orig);
            }
        }

        public void DeleteTreeNodePropertyBinding(TreeNodePropertyBinding treeNodePropertyBinding)
        {
            if ((treeNodePropertyBinding.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodePropertyBinding, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.TreeNodePropertyBindings.Attach(treeNodePropertyBinding);
                this.ObjectContext.TreeNodePropertyBindings.DeleteObject(treeNodePropertyBinding);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'TreeNodeQueries' query.
        [Query(IsDefault = true)]
        public IQueryable<TreeNodeQuery> GetTreeNodeQueries()
        {
            return this.ObjectContext.TreeNodeQueries.Include("TreeNodeBase");
        }

        public void InsertTreeNodeQuery(TreeNodeQuery treeNodeQuery)
        {
            if ((treeNodeQuery.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeQuery, EntityState.Added);
            }
            else
            {
                this.ObjectContext.TreeNodeQueries.AddObject(treeNodeQuery);
            }
        }

        public void UpdateTreeNodeQuery(TreeNodeQuery currentTreeNodeQuery)
        {
            var orig = this.ChangeSet.GetOriginal(currentTreeNodeQuery);

            if (orig == null)
            {
                this.ObjectContext.TreeNodeQueries.AttachAsModified(currentTreeNodeQuery);
            }
            else
            {
                this.ObjectContext.TreeNodeQueries.AttachAsModified(currentTreeNodeQuery, orig);
            }
        }

        public void DeleteTreeNodeQuery(TreeNodeQuery treeNodeQuery)
        {
            if ((treeNodeQuery.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeQuery, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.TreeNodeQueries.Attach(treeNodeQuery);
                this.ObjectContext.TreeNodeQueries.DeleteObject(treeNodeQuery);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'TreeNodeRootSources' query.
        [Query(IsDefault = true)]
        public IQueryable<TreeNodeRootSource> GetTreeNodeRootSources()
        {
            return this.ObjectContext.TreeNodeRootSources.Include("TreeNodeBase");
        }

        public void InsertTreeNodeRootSource(TreeNodeRootSource treeNodeRootSource)
        {
            if ((treeNodeRootSource.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeRootSource, EntityState.Added);
            }
            else
            {
                this.ObjectContext.TreeNodeRootSources.AddObject(treeNodeRootSource);
            }
        }

        public void UpdateTreeNodeRootSource(TreeNodeRootSource currentTreeNodeRootSource)
        {
            var orig = this.ChangeSet.GetOriginal(currentTreeNodeRootSource);

            if (orig == null)
            {
                this.ObjectContext.TreeNodeRootSources.AttachAsModified(currentTreeNodeRootSource);
            }
            else
            {
                this.ObjectContext.TreeNodeRootSources.AttachAsModified(currentTreeNodeRootSource, orig);
            }
        }

        public void DeleteTreeNodeRootSource(TreeNodeRootSource treeNodeRootSource)
        {
            if ((treeNodeRootSource.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(treeNodeRootSource, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.TreeNodeRootSources.Attach(treeNodeRootSource);
                this.ObjectContext.TreeNodeRootSources.DeleteObject(treeNodeRootSource);
            }
        }

        public IQueryable<AbstraXProvider> GetAbstraXProviders()
        {
            return this.ObjectContext.AbstraXProviders;
        }

        public void InsertAbstraXProvider(AbstraXProvider abstraXProvider)
        {
            if ((abstraXProvider.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(abstraXProvider, EntityState.Added);
            }
            else
            {
                this.ObjectContext.AbstraXProviders.AddObject(abstraXProvider);
            }
        }

        public void UpdateAbstraXProvider(AbstraXProvider currentAbstraXProvider)
        {
            this.ObjectContext.AbstraXProviders.AttachAsModified(currentAbstraXProvider, this.ChangeSet.GetOriginal(currentAbstraXProvider));
        }

        public void DeleteAbstraXProvider(AbstraXProvider abstraXProvider)
        {
            if ((abstraXProvider.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(abstraXProvider, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.AbstraXProviders.Attach(abstraXProvider);
                this.ObjectContext.AbstraXProviders.DeleteObject(abstraXProvider);
            }
        }
    }
}


